package com.biyabi.service.impl;

import com.biyabi.dao.CommentMapper;
import com.biyabi.dao.CouponMapper;
import com.biyabi.dao.UserCouponMapper;
import com.biyabi.entity.*;
import com.biyabi.pojo.CouponsData;
import com.biyabi.service.CouponService;
import com.github.pagehelper.PageHelper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

@Service
@Transactional
public class CouponServiceImpl implements CouponService {
    @Resource
    private CouponMapper couponMapper;
    @Resource
    private UserCouponMapper userCouponMapper;
    @Resource
    private CommentMapper commentMapper;
    @Transactional(propagation = Propagation.SUPPORTS,readOnly = true)
    public CouponsData findAll(Integer currentPage, Integer pageSize,String columnName,String columnValue){
        currentPage = currentPage == null ? 1:currentPage;
        pageSize= pageSize== null ? 20:pageSize;
        CouponExample couponExample = new CouponExample();
        if(columnName!=null && columnValue!=null){
            switch (columnName){
                case "name":
                    couponExample.createCriteria().andNameLike("%"+columnValue+"%");
                    break;
                case"quota":
                    couponExample.createCriteria().andQuotaEqualTo(Integer.parseInt(columnValue));
                    break;
                case"sill":
                    couponExample.createCriteria().andSillEqualTo(Double.parseDouble(columnValue));
                    break;
                case"validity":
                    couponExample.createCriteria().andValidityEqualTo(Integer.parseInt(columnValue));
                    break;
                case"count":
                    couponExample.createCriteria().andCountEqualTo(Integer.parseInt(columnValue));
                    break;
                case"discount":
                    couponExample.createCriteria().andDiscountEqualTo(Double.parseDouble(columnValue));
                    break;
            }
        }
        CouponsData couponsData = new CouponsData();
        couponsData .setTotal(couponMapper.countByExample(couponExample));
        PageHelper.startPage(currentPage,pageSize);
        List<Coupon> brands = couponMapper.selectByExample(couponExample);
        couponsData .setRows(brands);
        return couponsData ;
    }
    public void add(Coupon coupon){
        coupon.setId(UUID.randomUUID().toString());
        couponMapper.insert(coupon);
    }
    public void update(Coupon coupon){
        couponMapper.updateByPrimaryKey(coupon);
    }

    @Override
    @Transactional(propagation = Propagation.SUPPORTS,readOnly = true)
    public Coupon findOne(String id) {
        return couponMapper.selectByPrimaryKey(id);
    }
    @Override
    @Transactional(propagation = Propagation.SUPPORTS,readOnly = true)
    public List<Coupon> findByUser(String userId){
        UserCouponExample userCouponExample = new UserCouponExample();
        userCouponExample.createCriteria().andUseridEqualTo(userId);
        List<UserCoupon> userCoupons = userCouponMapper.selectByExample(userCouponExample);
        List<String> couponIds = new ArrayList<>();
        for (UserCoupon userCoupon : userCoupons) {
            couponIds.add(userCoupon.getCouponid());
        }
        CouponExample example = new CouponExample();
        example.createCriteria().andIdIn(couponIds);
        return couponMapper.selectByExample(example);
    }
}
